package com.lyl.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lyl.springcloud.handle.CustomerBlockHandler;
import com.lyl.springcloud.model.Payment;
import com.lyl.springcloud.model.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author lylbell
 * @date 2021-06-06 20:17
 */
@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public Result byResource() {
        return new Result(200,"按资源名称限流测试ok",new Payment(1L,"serial0001"));
    }

    //byResource的降级处理方法。
    public Result handleException(BlockException exception) {
        return new Result(444,exception.getClass().getCanonicalName() + "\t服务不可用");
    }


        @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public Result byUrl() {
        return new Result(200,"按url限流测试ok",new Payment(2L,"serial0002"));
    }

    @GetMapping("/rateLimit/customerBlockHandler")
    //blockHandlerClass:全局处理的类名，blockHandler：降级处理对应的方法
    @SentinelResource(value = "customerBlockHandler",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handlerException2")
    public Result customerBlockHandler() {
        return new Result(200,"按客户自定义",new Payment(3L,"serial0003"));
    }
}
